package com.example.web.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 验证码
 */
@WebServlet("/checkCode")
public class CheckCodeServlet extends HttpServlet {
    //图片的宽高
    int width = 100;
    int height = 30;
    //1.创建一个对象，用于在内存中生成验证码的图片对象
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();  //获取图片的画笔对象

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.美化验证码
        this.beautifyCode();
        //3.将图片输出到页面展示
        ServletOutputStream outputStream = resp.getOutputStream();
        ImageIO.write(image, "jpg", outputStream);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    //美化验证码
    private void beautifyCode() {
        /**
         * 整体逻辑
         */
        this.fillBg();
        this.drawBorder();
        this.ranCode();
        this.ranLine();
    }


    //填充背景色
    private void fillBg() {
        graphics.setColor(this.getRandomColor()); //设置画笔颜色
        graphics.fillRect(0, 0, width, height); //填充背景色
    }

    //画边框
    private void drawBorder() {
        graphics.setColor(this.getRandomColor());
        graphics.drawRect(0, 0, width - 1, height - 1);
    }

    //随机生成4位验证码
    private void ranCode() {
        //2.3写验证码
        String str = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
        //创建随机角标
        Random random = new Random();
        for (int i = 1; i <= 4; i++) {
            int index = random.nextInt(str.length());
            //获取字符
            char c = str.charAt(index); //随机字符
            //写验证码
            graphics.drawString(c + "", width / 5 * i,20);
            graphics.setColor(this.getRandomColor());
            graphics.setFont(new Font("黑体", Font.BOLD, 25));//验证码字体
        }
    }

    //随机生成干扰线
    private void ranLine() {
        Random random = new Random();
        graphics.setColor(this.getRandomColor());
        //随机生成坐标点
        for (int i = 1; i <= 5; i++) {
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int y2 = random.nextInt(height);
            graphics.drawLine(x1, y1, x2, y2);
        }
    }

    //生成随机颜色
    private Color getRandomColor() {
        Random color = new Random();
        int r = color.nextInt(255);
        int g = color.nextInt(255);
        int b = color.nextInt(255);
        Color c = new Color(r, g, b);
        return c;
    }
}
